package com.ccx.myinterpreter;

import java.util.HashMap;
import java.util.Map;

public class LoxInstance {

    private Map<String, Object> fields = new HashMap<>();
    private LoxClass cls;

    public LoxInstance(LoxClass cls) {
        this.cls = cls;
    }

    public Object get(String key) {
        Object obj = fields.get(key);
        if(obj == null) {
            LoxFun loxFun = cls.findMethod(key);
            if(loxFun != null) {
                obj = loxFun.bindThis(this);
            }
        }
        return obj;
    }

    public Map<String, Object> getFields() {
        return fields;
    }
    public void setFields(Map<String, Object> fields) {
        this.fields = fields;
    }
    public LoxClass getCls() {
        return cls;
    }
    public void setCls(LoxClass cls) {
        this.cls = cls;
    }
    
}
